/*
 *  $Id: GrantLife.cs 1051 2010-05-18 22:10:41Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

namespace Ragima.Cards001.Earth.Spells {
    public class GrantLife: SpellCard<GraveyardData> {
        public GrantLife(): base("001_e_s_grant_life") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Earth;
            Value = 100;
            Target = GraveyardTarget.OwnResurrect;
            AddManaCost(ElementType.Earth, 3);
            AddCondition(new CreaturesCondition(true, 1));
            AddCondition(new GraveyardCondition(false, false, 1));
            AddCondition(new AffectCondition<AvatarOfNature.AvatarOfNatureAffect>(GetConditionPhrase(), true));
        }

        protected override void DoAction(TargetArgs<GraveyardData> args) {
            AvatarOfNature.UseAffect(args.Player);
            int place = args.Player.GetRandomFreePlace();
            if (place < 0) return;
            Creature cr = args.Player.ResurrectCreature(args.Target.Item, place, true);
            if (cr == null) return;
            cr.AddAffect<GrantLifeAffect>();
        }

        private class GrantLifeAffect: SpellAffect<GrantLife> {
            public GrantLifeAffect(): base(true) {
                DefaultDescription = true;
            }

            public override void SetAffect()  {
                base.SetAffect();
                LocalSummary.OnDeath += DoDeath;
            }
            public override void ResetAffect() {
                base.ResetAffect();
                LocalSummary.OnDeath -= DoDeath;
            }

            public override void Dispell() {
                DoDeath(Owner);
                base.Dispell();
            }

            private void DoDeath(AbstractObject obj) {
                Creature cr = obj as Creature;
                if (cr != null) cr.ReturnToHand();
            }
        }
    }
}
